﻿using Autofac.Extensions.DependencyInjection;
using Autofac;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using Serilog;
using xjjxmm.infrastructure.Authorizations;
using xjjxmm.infrastructure.repository.impl.sugar;
using xjjxmm.infrastructure.repository.interfaces;
using xjjxmm.infrastructure.Webs;
using xjjxmm.infrastructure.xjjxmmLog.LogKit;
using XjjXmm.Infrastructure.Configuration;
using XjjXmm.Infrastructure.Constant;
using XjjXmm.Infrastructure.Jwt;
using XjjXmm.Infrastructure.User;
using XjjXmm.Infrastructure.Webs.ActionResult;
using MongoDB.Driver;
using XjjXmm.Infrastructure.Mongo;

namespace xjjxmm.infrastructure.compont;

public class MogoCompont : ICompont
{
    public int Order { get; set; } = 100;

    public bool Regist(ContainerBuilder builder)
    {
        if (!ConfigHelper.GetConfig(CommonConstant.MongoConfigKey).IsNullOrEmpty())
        {
            builder.RegisterGeneric(typeof(MongoRepository<>)).InstancePerLifetimeScope();

            builder.Register(com =>
            {
                var connectionString = ConfigHelper.GetConfig("mongodb:connectiongString");
                var database = ConfigHelper.GetConfig("mongodb:database");
                var client = new MongoClient(connectionString);
                return client.GetDatabase(database);

            }).AsImplementedInterfaces().SingleInstance();

            return true;
        }

        return false;
    }
}
